package me.zhengjie.business.service.impl;

import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.business.domain.Zhsq;
import me.zhengjie.business.domain.vo.HljgDspVo;
import me.zhengjie.business.repository.ZhsqRepository;
import me.zhengjie.business.service.PgfyProService;
import me.zhengjie.config.DebugProperties;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.StoredProcedureQuery;
import java.util.ArrayList;
import java.util.List;

/**
 * @description 调用居家评估费用存储过程
 * @author xiahongbo
 * @date 2022-08-03
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class PgfyProServiceImpl implements PgfyProService {

    private final ZhsqRepository zhsqRepository;
    @Autowired
    private EntityManager entityManager;

    @Override
    public JSONObject jzdj2210(Long userId, Long zhid) {

        JSONObject object = new JSONObject();
        log.info("自评审批通过--生成就诊登记存储过程 开始 zhid:{}", zhid);

        /**
         * StoredProcedureQuery 创建存储过程仓库
         * createStoredProcedureQuery（name） name 时过程名
         * registerStoredProcedureParameter表示存储过程中注册的参数
         * ParameterMode 参数
         */
        StoredProcedureQuery storedProcedureQuery = this.entityManager.createNamedStoredProcedureQuery("makeJzdj93");

        /**
         * 设置传入参数值
         */
        storedProcedureQuery.setParameter("IR_FWLX", "93");
        storedProcedureQuery.setParameter("IR_ORDER_ID", "");
        storedProcedureQuery.setParameter("IR_USER_ID", String.valueOf(userId));
        storedProcedureQuery.setParameter("IR_ZHID", String.valueOf(zhid));
        /**
         * 获取结果出参
         */
        storedProcedureQuery.execute();
        int code = (int) storedProcedureQuery.getOutputParameterValue("OR_APPCODE");
        String msg = (String) storedProcedureQuery.getOutputParameterValue("OR_MESSAGE");

        log.info("自评审批通过-生成就诊登记存储过程 结束 :code:{}，msg:{}", code, msg);
        object.put("code", code);
        object.put("msg", msg);

        return object;
    }

    @Override
    public JSONObject fymx2310(Long userId, Long zhid) {

        JSONObject object = new JSONObject();
        log.info("自评审批通过--生成费用明细存储过程 开始 zhid:{}", zhid);

        /**
         * StoredProcedureQuery 创建存储过程仓库
         * createStoredProcedureQuery（name） name 时过程名
         * registerStoredProcedureParameter表示存储过程中注册的参数
         * ParameterMode 参数
         */
        StoredProcedureQuery storedProcedureQuery = this.entityManager.createNamedStoredProcedureQuery("makeFymx93");

        /**
         * 设置传入参数值
         */
        storedProcedureQuery.setParameter("IR_FWLX", "93");
        storedProcedureQuery.setParameter("IR_ORDER_ID", "");
        storedProcedureQuery.setParameter("IR_USER_ID", String.valueOf(userId));
        storedProcedureQuery.setParameter("IR_ZHID", String.valueOf(zhid));
        /**
         * 获取结果出参
         */
        storedProcedureQuery.execute();
        int code = (int) storedProcedureQuery.getOutputParameterValue("OR_APPCODE");
        String msg = (String) storedProcedureQuery.getOutputParameterValue("OR_MESSAGE");

        log.info("自评审批通过-生成费用明细存储过程 结束 :code:{}，msg:{}", code, msg);
        object.put("code", code);
        object.put("msg", msg);

        return object;
    }

    @Override
    public JSONObject fyjs2410(Long userId, Long zhid) {

        JSONObject object = new JSONObject();

        log.info("自评审批通过--生成费用结算存储过程 开始 zhid:{}", zhid);

        /**
         * StoredProcedureQuery 创建存储过程仓库
         * createStoredProcedureQuery（name） name 时过程名
         * registerStoredProcedureParameter表示存储过程中注册的参数
         * ParameterMode 参数
         */
        StoredProcedureQuery storedProcedureQuery = this.entityManager.createNamedStoredProcedureQuery("makeFyjs93");

        /**
         * 设置传入参数值
         */
        storedProcedureQuery.setParameter("IR_USER_ID", String.valueOf(userId));
        storedProcedureQuery.setParameter("IR_ZHID", String.valueOf(zhid));
        /**
         * 获取结果出参
         */
        storedProcedureQuery.execute();
        int code = (int) storedProcedureQuery.getOutputParameterValue("OR_APPCODE");
        String msg = (String) storedProcedureQuery.getOutputParameterValue("OR_MESSAGE");

        log.info("自评审批通过-生成费用结算存储过程 结束 :code:{}，msg:{}", code, msg);
        object.put("code", code);
        object.put("msg", msg);

        return object;
    }

    @Override
    public JSONObject exeSiInterface(Long zhid) {

        Zhsq zhsq = zhsqRepository.findById(zhid).orElseGet(Zhsq::new);
        log.info("queryTodo startEnd : {} ", "");
        // 使用预编译防止sql注入
        String sql = "select t.input from mnt_ybc t where t.zt='0' and ywbm='2210' and t.ywid= :zhid order by t.intime";
        Query query = entityManager.createNativeQuery(sql);
        query.setParameter("zhid", zhid);
        List result = query.getResultList();
        for (Object obj : result) {
            Object[] arr = (Object[]) obj;
            String y2210 = (String)arr[0];
            log.info("y2210: {}, grbh: {} ", y2210, zhsq.getBisZhsqGrbh());
        }

        return null;
    }
}
